package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.TextField;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;

import Event.EventCalendar;
import Reservation.ReserveCalendar;
import Signup.Account;
import Signup.AccountManagement;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class Login extends JFrame {

	private JPanel contentPane;
	private JTextField tfid;
	private TextField tfpw;
	private JButton btnLogin;
	static int nowUser;

	public Login() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setSize(975, 770);
		setLocationRelativeTo(null);
		contentPane = new JPanel() {
			public void paintComponent(Graphics g) {
				Image image = new ImageIcon("src/source/image/login.png")
						.getImage();
				g.drawImage(image, 0, 0, this);
			};
		};

		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		getContentPane().setLayout(null);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		JLabel lblInsertId = new JLabel("Insert ID:");
		lblInsertId.setBounds(260, 288, 57, 15);
		getContentPane().add(lblInsertId);

		JLabel lblInsertPassword = new JLabel("Insert Password");
		lblInsertPassword.setBounds(260, 395, 116, 15);
		getContentPane().add(lblInsertPassword);

		tfid = new JTextField();
		tfid.setBounds(433, 285, 116, 21);
		getContentPane().add(tfid);
		tfid.setColumns(10);

		tfpw = new TextField();
		tfpw.setBounds(433, 392, 116, 21);
		tfpw.setEchoChar('*');
		getContentPane().add(tfpw);
		tfpw.setColumns(10);
		contentPane.setLayout(null);

		btnLogin = new JButton("LOGIN");
		btnLogin.setBounds(356, 514, 97, 23);
		contentPane.add(btnLogin);
		btnLogin.addActionListener(new LoginAction());
		setVisible(true);
	}

	public static void showInfoBox(String msg, String title) {
		JOptionPane.showMessageDialog(null, msg, title,
				JOptionPane.INFORMATION_MESSAGE);
	}

	class LoginAction implements ActionListener {

		public void actionPerformed(ActionEvent e) {

			FileInputStream fileStream = null;
			ObjectInputStream os = null;
			ArrayList<Account> alist = null;

			try {
				fileStream = new FileInputStream("accountdata.ser");
				os = new ObjectInputStream(fileStream);
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
				Login.showInfoBox("Please sign up.", "error");
				SignUp su = new SignUp();
				su.setVisible(true);
				su.setLocationRelativeTo(null);
			} catch (IOException e1) {
				e1.printStackTrace();
			}

			try {
				Object list = os.readObject();
				alist = (ArrayList<Account>) list;

			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}

			for (Account a : alist) {
				if (tfid.getText().equals(a.getId())) {
					if (tfpw.getText().equals(a.getPassword())) {
						Login.showInfoBox("Login Success.", "Login");
						setVisible(false);
						nowUser = alist.indexOf(a);
						AccountManagement.User = alist.get(nowUser);
						ReserveCalendar rc = new ReserveCalendar();
						rc.setVisible(true);
						rc.setLocationRelativeTo(null);

					} else {
						Login.showInfoBox("Wrong Password.", "Login");
						tfpw.requestFocus();
						tfpw.selectAll();
					}

				} else if (!tfid.getText().equals(a.getId())) {
					JOptionPane.showMessageDialog(null, "Please Sign Up.",
							"Reservation", JOptionPane.INFORMATION_MESSAGE);
					setVisible(false);
					setLocationRelativeTo(null);
					(new SignUp()).setVisible(true);
				}
			}

		}
	}

}